package com.fenfen.novel.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fenfen.novel.R;
import com.fenfen.novel.model.Category;
import com.fenfen.novel.utils.CommonUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.CategoryHolderView> {

  private static String Tag = CategoryAdapter.class.getSimpleName();

  private Context mContext;
  private List<Category> mData = new ArrayList<>();
  private CategoryClickListener mItemClickListener;

  private int colorAccent;

  public CategoryAdapter(Context context) {
    this.mContext = context;
    colorAccent = CommonUtil.resolveColor(mContext, R.attr.colorAccent);
  }

  public CategoryAdapter(Context context, List<Category> list) {
    this(context);
    setData(list);
  }

  @Override
  public CategoryHolderView onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.novel_category_item, parent, false);
    CategoryHolderView holder = new CategoryHolderView(view);
    return holder;
  }

  @Override
  public void onBindViewHolder(final CategoryHolderView holder, final int position) {
    Category category = mData.get(position);
    holder.categoryTitle.setText(category.getName());
    String coverlUrl = category.getImg();
    if (mContext != null && !TextUtils.isEmpty(coverlUrl)) {
      coverlUrl = CommonUtil.PRE_COVER_URL + coverlUrl;
      Picasso.with(mContext).load(coverlUrl).placeholder(R.drawable.nocover).into(holder.categoryCover);
    }
    holder.itemView.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            holder.categoryTitle.setTextColor(ContextCompat.getColor(mContext, R.color.md_grey_500));
            holder.categoryCover.setAlpha(0.5f);
            break;
          case MotionEvent.ACTION_UP:
          case MotionEvent.ACTION_CANCEL:
          case MotionEvent.ACTION_HOVER_EXIT:
            holder.categoryTitle.setTextColor(colorAccent);
            holder.categoryCover.setAlpha(1.0f);
            break;
        }
        return false;
      }
    });
    holder.itemView.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        if (mItemClickListener != null) {
          mItemClickListener.onItemClick(v, mData.get(position));
        }
      }
    });
  }

  public void setData(List<Category> list) {
    if (list != null && list.size() > 0) {
      mData.clear();
      mData.addAll(list);
      notifyDataSetChanged();
    }
  }

  public void addData(List<Category> list) {
    if (list != null && list.size() > 0) {
      int positionStart = mData.size();
      mData.addAll(list);
//      notifyDataSetChanged();
      notifyItemRangeInserted(positionStart, list.size());
    }
  }

  @Override
  public int getItemCount() {
    return mData.size();
  }

  public List<Category> getData() {
    return mData;
  }

  public class CategoryHolderView extends RecyclerView.ViewHolder {
    private TextView categoryTitle;
    private ImageView categoryCover;

    public CategoryHolderView(View itemView) {
      super(itemView);
      categoryTitle = (TextView) itemView.findViewById(R.id.category_title);
      categoryCover = (ImageView) itemView.findViewById(R.id.category_cover);
    }
  }

  public void setItemClickListener(CategoryClickListener mItemClickListener) {
    this.mItemClickListener = mItemClickListener;
  }

  public interface CategoryClickListener {
    void onItemClick(View v, Category category);
  }

}

